import 'package:flutter/material.dart';
import 'package:qunaimedical/components/primary_button.dart';
import 'package:qunaimedical/utils/toast_util.dart';

/// 字段修改界面
class FieldEditPage extends StatefulWidget {
  final String? value;
  final FieldEditType type;

  const FieldEditPage({Key? key, this.value, required this.type})
      : super(key: key);

  @override
  _FieldEditPageState createState() => _FieldEditPageState();
}

enum FieldEditType {
  name,
  hospital,
  department,
  position,
  skill,
  resume,
}

class _FieldEditPageState extends State<FieldEditPage> {
  late String titleText;
  TextEditingController _controller = TextEditingController();

  /// 是否显示删除图标
  bool _hideSuffixIcon = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(titleText)),
      body: ListView(
        children: [
          _buildInput(),
          PrimaryButton(
            '确认',
            onPressed: () {
              String content = _controller.text.trim();
              if (content.isEmpty) {
                ToastUtil.show('内容不能为空！');
              } else {
                Navigator.pop(context, content);
              }
            },
          ),
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _controller.text = widget.value ?? "";
    switch (widget.type) {
      case FieldEditType.name:
        titleText = '更改姓名';
        break;
      case FieldEditType.hospital:
        titleText = '更改医院名称';
        break;
      case FieldEditType.hospital:
        titleText = '更改医院名称';
        break;
      case FieldEditType.department:
        titleText = '更改科室';
        break;
      case FieldEditType.position:
        titleText = '更改职称';
        break;
      case FieldEditType.skill:
        titleText = '更改擅长领域';
        break;
      case FieldEditType.resume:
        titleText = '更改基本简历';
        break;
    }
  }

  Widget _buildInput() {
    var formatters;
    var keyboardType = TextInputType.text;
    var maxLength = 20;
    int maxLines = 1;
    if (widget.type == FieldEditType.skill ||
        widget.type == FieldEditType.resume) {
      maxLength = 200;
      maxLines = 8;
    }
    return Container(
      margin: EdgeInsets.only(top: 20, bottom: 20),
      child: TextField(
        autocorrect: false,
        controller: _controller,
        keyboardType: keyboardType,
        inputFormatters: formatters,
        maxLength: maxLength,
        maxLines: maxLines,
        decoration: InputDecoration(
          fillColor: Colors.white,
          contentPadding: EdgeInsets.all(16),
          filled: true,
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(0)),
            borderSide: BorderSide(color: Colors.transparent),
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(0)),
            borderSide: BorderSide(color: Colors.transparent),
          ),
          suffixIcon: Offstage(
            offstage: _hideSuffixIcon,
            child: IconButton(
                icon: Icon(
                  Icons.highlight_off,
                  color: Colors.grey,
                ),
                onPressed: () {
                  _controller.clear();
                  setState(() {
                    _hideSuffixIcon = true;
                  });
                }),
          ),
        ),
        onChanged: (value) {
          setState(() {
            _hideSuffixIcon = value.isEmpty;
          });
        },
      ),
    );
  }
}
